/**
 * 初始化贷款产品详情对话框  /^\+?[1-9]\d*$/
 */
var TheTestInfoDlg = {
    theTestInfoData: {},
    validateFields: {}
}
/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TheTestInfoDlg.set = function (key, val) {
    this.theTestInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}
/**
 * 关闭此对话框
 */
TheTestInfoDlg.close = function () {
    parent.layer.close(window.parent.TheTest.layerIndex);
}
/**
 * 验证数据是否为空
 */
TheTestInfoDlg.validate = function () {
    $('#addForm').data("bootstrapValidator").resetForm();
    $('#addForm').bootstrapValidator('validate');
    return $("#addForm").data('bootstrapValidator').isValid();
};
/**
 * 收集数据
 */
TheTestInfoDlg.collectData = function () {
    this.set('id').set('theTestName').set("passWord").set("timeLength").set("remark").set("scoresOfAdoption")
        .set('typeOfQuestion').set("numberOfQuestion").set("scoresOfQuestion");
}
/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TheTestInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 清除数据
 */
TheTestInfoDlg.clearData = function () {
    this.theTestInfoData = {};
}
/**
 * 提交添加
 */
TheTestInfoDlg.addSubmit = function () {
    var theTestName = $('#theTestName').val();
    var passWord = $('#passWord').val();
    var timeLength = $('#timeLength').val();
    var remark = $('#remark').val();
    var typeOfQuestion = $('#typeOfQuestion').val();
    var scoresOfAdoption = $('#scoresOfAdoption').val();
    var numberOfQuestion = $('#numberOfQuestion').val();
    var scoresOfQuestion = $('#scoresOfQuestion').val();
    if (theTestName == "" || theTestName == null) {
        alert("请输入考试名称！");
    }
    else if (passWord == "" || passWord == null) {
        alert("请输入考试密码！");
    }
    else if (numberOfQuestion == "" || numberOfQuestion == null) {
        alert("请输入题目数量！");
    }
    else if (scoresOfAdoption == "" || scoresOfAdoption == null) {
        alert("请输入通过分数！");
    }
    else if (scoresOfQuestion == "" || scoresOfQuestion == null) {
        alert("请输入每题分数！");
    }
    else if (passWord.length > 10) {
        alert("密码小于10！");
    }
    else if (!TheTestInfoDlg.isPositiveInteger(numberOfQuestion)) {
        alert("题目数量为整数");
    }
    else if (isNaN(scoresOfQuestion)) {
        alert("每题分数为数字");
    }
    else if (isNaN(scoresOfAdoption)) {
        alert("通过分数为数字");
    }
    else if (theTestName.length > 100) {
        alert("考试名称小于100！");
    }
    else if (remark.length > 100) {
        alert("备注长度小于100！");
    } else {
        this.clearData();
        this.collectData();
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/theTest/add", function (data) {
            if (data == 0) {
                Feng.error("考试名称已存在！请修改");
            } else {
                Feng.success("添加成功!");
                window.parent.TheTest.table.refresh();
                TheTestInfoDlg.close();
            }
        }, function (data) {
            Feng.error("添加失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.theTestInfoData);
        ajax.start();
    }
}

//初始化数据
TheTestInfoDlg.initData = function () {
    //状态单选
    var seletimeLength = $('#itimeLength').val();
    if ($('#id').val() != "") {
        $("#timeLength option[value=" + seletimeLength + "]").attr("selected", "selected");
    }
}
TheTestInfoDlg.isPositiveInteger = function (s) {//是否为正整数
    var re = /^[0-9]+$/;
    return re.test(s)
}
/**
 * 提交修改
 */
TheTestInfoDlg.editSubmit = function () {
    var theTestName = $('#theTestName').val();
    var passWord = $('#passWord').val();
    var timeLength = $('#timeLength').val();
    var remark = $('#remark').val();
    var typeOfQuestion = $('#typeOfQuestion').val();
    var scoresOfAdoption = $('#scoresOfAdoption').val();
    var numberOfQuestion = $('#numberOfQuestion').val();
    var scoresOfQuestion = $('#scoresOfQuestion').val();
    if (theTestName == "" || theTestName == null) {
        alert("请输入考试名称！");
    }
    else if (passWord == "" || passWord == null) {
        alert("请输入考试密码！");
    }
    else if (numberOfQuestion == "" || numberOfQuestion == null) {
        alert("题目数量为正整数");
    }
    else if (scoresOfQuestion == "" || scoresOfQuestion == null) {
        alert("请输入每题分数！");
    }
    else if (scoresOfAdoption == "" || scoresOfAdoption == null) {
        alert("请输入通过分数！");
    }
    else if (passWord.length > 10) {
        alert("密码小于10！");
    }
    else if (theTestName.length > 100) {
        alert("考试名称小于100！");
    }
    else if (remark.length > 100) {
        alert("备注长度小于100！");
    }
    else if (!TheTestInfoDlg.isPositiveInteger(numberOfQuestion)) {
        alert("题目数量为整数");
    }
    else if (isNaN(scoresOfQuestion)) {
        alert("每题分数为数字");
    }
    else if (isNaN(scoresOfAdoption)) {
        alert("通过分数为数字");
    }
    else if (theTestName.length > 100) {
        alert("考试名称小于100！");
    }
    else if (remark.length > 100) {
        alert("备注长度小于100！");
    } else {
        this.clearData();
        this.collectData();
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/theTest/update", function (data) {
            if (data == 0) {
                Feng.error("考试名称已存在！请修改");
            } else {
                Feng.success("修改成功!");
                window.parent.TheTest.table.refresh();
                TheTestInfoDlg.close();
            }
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.theTestInfoData);
        ajax.start();
    }
}

$(function () {
    TheTestInfoDlg.initData();
});
